var eap_manager_eventItem = {
	init: function() {
		$('[name="model.contacter.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
				req.filterProperty = 'parent.id';
				req.filter = $('[name="model.party.id"]', getActiveDom()).val();
				if (req.filter == '') {
					req.cancel = true;
				} 
				return req.pageNo + ',' + req.term + ',' + req.filter;
			});
		
		getActiveDom().on('blur', '[cmbname="model.party.id"]', function() {
			// show the /sale/customer.getAccountInfo
			var $this = $(this);
			var partyId = $this.closest('.jcombobox').find('[name="model.party.id"]').val();
			var oldVal = $this.data('oldVal');
			if (partyId != '' && partyId != oldVal) {
				$this.data('oldVal', partyId);
				var url = g_base_site + '/espirit/sale/customer._update.html?_resultCode=$json&target=execute'
					+ '&properties=currency.id,currency.name,taxRate.id,taxRate.code,paymentTerm.id,paymentTerm.name,paymentMode.id,paymentMode.name,deliveryTerm.id,deliveryTerm.name,deliveryMode.id,deliveryMode.name,defaultContacter.id,defaultContacter.name&model.id=' + partyId;
				var $tr = $this.closest('tr');
				$.getJSON(url, function(jsonData) {
					$('[name="model.contacter.id"]', getActiveDom()).val(jsonData['defaultContacter'].id).next('[cmbname]').val(jsonData['defaultContacter'].name);
					$('[name="model.currency.id"]', getActiveDom()).val(jsonData.currency.id).next('[cmbname]').val(jsonData.currency.name);
					$('[name="model.taxRate.id"]', getActiveDom()).val(jsonData['taxRate'].id).next('[cmbname]').val(jsonData['taxRate'].code);
					$('[name="model.paymentTerm.id"]', getActiveDom()).val(jsonData.paymentTerm.id).next('[cmbname]').val(jsonData.paymentTerm.name);
					$('[name="model.paymentMode.id"]', getActiveDom()).val(jsonData.paymentMode.id).next('[cmbname]').val(jsonData.paymentMode.name);
					$('[name="model.deliveryMode.id"]', getActiveDom()).val(jsonData.deliveryMode.id).next('[cmbname]').val(jsonData.deliveryMode.name);
					$('[name="model.deliveryTerm.id"]', getActiveDom()).val(jsonData.deliveryTerm.id).next('[cmbname]').val(jsonData.deliveryTerm.name);
				});
			}
		});
		
		getActiveDom().on('blur', '[name="model.qty"],[_name="model.price"]', function() {
			var qty = $('[name="model.qty"]', getActiveDom()).val();
			var price =  $('[name="model.price"]', getActiveDom()).val();
			if (qty != '' && price != '') {
				qty = Number(qty);
				price = Number(price);
				var total = qty * price;
				$('[txtname="model.amount"]', getActiveDom()).html(formatPrice(total));
			}
		});
		
		getActiveDom().on('blur', '[cmbname="model.intPartNumber"]', function() {
			var $this = $(this);
			var partNumber = $this.val();
			var oldVal = $this.data('oldVal');
			if (partNumber != '' && partNumber != oldVal) {
				$this.data('oldVal', partNumber);
				var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByPartNumber.html?_resultCode=$json&target=execute'
					+ '&properties=brand.id,brand.name,eccn.id,eccn.code&partNumber=' + encodeURIComponent(partNumber);
				$.getJSON(url, function(jsonData) {
					$('[cmbname="model.brand.id"]', getActiveDom()).val(jsonData.brand.name);
					$('[name="model.brand.id"]', getActiveDom()).val(jsonData.brand.id);
					$('[cmbname="model.eccn.id"]', getActiveDom()).val(jsonData.eccn.code);
					$('[name="model.eccn.id"]', getActiveDom()).val(jsonData.eccn.id);
					$('[name="model.brandName"]', getActiveDom()).val(jsonData.brand.name).next('[cmbname]').val(jsonData.brand.name);;
				});
			}
		});
		
		getActiveDom().on('blur', '[cmbname="model.partItem.id"]', function() {
			var $this = $(this);
			var partNumber = $this.val();
			var oldVal = $this.data('oldVal');
			if (partNumber != '' && partNumber != oldVal) {
				$this.data('oldVal', partNumber);
				var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByPartNumber.html?_resultCode=$json&target=execute'
					+ '&properties=mfrNumber,brand.id,brand.name&partNumber=' + encodeURIComponent(partNumber);
				$.getJSON(url, function(jsonData) {
					$('[cmbname="model.partItem.brand.id"]', getActiveDom()).val(jsonData.brand.name);
					$('[cmbname="model.partItem.mfrNumber"]', getActiveDom()).val(jsonData.mfrNumber);
				});
			}
		});
		
		getActiveDom().on('blur', '[cmbname="model.partItem.partNumber"]', function() {
			var $this = $(this);
			var partNumber = $this.val();
			var oldVal = $this.data('oldVal');
			if (partNumber != '' && partNumber != oldVal) {
				$this.data('oldVal', partNumber);
				var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByPartNumber.html?_resultCode=$json&target=execute'
					+ '&properties=mfrNumber,brand.id,brand.name&partNumber=' + encodeURIComponent(partNumber);
				$.getJSON(url, function(jsonData) {
					$('[cmbname="model.partItem.brand.id"]', getActiveDom()).val(jsonData.brand.name);
					$('[cmbname="model.partItem.mfrNumber"]', getActiveDom()).val(jsonData.mfrNumber);
				});
			}
		});
		
		getActiveDom().on('blur', '[cmbname="model.partItem.mfrNumber"]', function() {
			var $this = $(this);
			var mfrNumber = $this.val();
			var oldVal = $this.data('oldVal');
			if (mfrNumber != '' && mfrNumber != oldVal) {
				$this.data('oldVal', mfrNumber);
				var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByMfrNumber.html?_resultCode=$json&target=execute'
					+ '&properties=id,partNumber,brand.id,brand.name&mfrNumber=' + encodeURIComponent(mfrNumber);
				$.getJSON(url, function(jsonData) {
					$('[cmbname="model.partItem.brand.id"]', getActiveDom()).val(jsonData.brand.name);
					$('[cmbname="model.partItem.id"]', getActiveDom()).val(jsonData.partNumber);
					$('[name="model.partItem.id"]', getActiveDom()).val(jsonData.id);
					
					$('[name="model.brandName"]', getActiveDom()).val(jsonData.brand.name).next('[cmbname]').val(jsonData.brand.name);;
					$('[name="model.partNumber"]', getActiveDom()).val(jsonData.partNumber).next('[cmbname]').val(jsonData.partNumber);;
				});
			}
		});
	}
}